package com.moto.controller.admin;

import com.moto.Vo.LogVo;
import com.moto.entityDto.EmpDto;
import com.moto.entityDto.LogDto;
import com.moto.entity.Emp;
import com.moto.entityDto.PageDto;
import com.moto.interceptor.JwtProperties;
import com.moto.interceptor.JwtUtil;
import com.moto.result.Result;
import com.moto.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.moto.service.IEmpService;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/emp")
@Slf4j
@CrossOrigin(origins = "*")
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private IEmpService empService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<LogVo> login(@RequestBody LogDto employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        System.out.println("员工登录");

        Emp emp = empService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("empId", emp.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        LogVo logVo = LogVo.builder()
                .id(emp.getId())
                .userName(emp.getUsername())
                .token(token)
                .build();
        //输出登录状态
        System.out.println(Result.success(logVo));
        return Result.success(logVo);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("退出账号")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     */
    @PostMapping("/saveUser")
    @ApiOperation("新增员工")
    public Result save(@RequestBody EmpDto empDto){
        log.info("新增员工：{}",empDto);
        empService.add(empDto);
        return Result.success();
    }
    /**
     * 分页查询员工
     */
    @GetMapping("/findAllEmp")
    @ApiOperation("查询员工信息")
    public Result<PageResult> findAllEmp(PageDto pageDto){
        log.info("查询员工信息：{}",pageDto);
        PageResult pageResult = empService.findAllId(pageDto);
        if (pageResult.getTotal()==0){
            return Result.error("没有员工信息");
        }
        return Result.success(pageResult);
    }
    /**
     * 员工删除信息
     */
    @DeleteMapping("/deleteEmpById/{id}")
    @ApiOperation("员工删除信息")
    public Result deleteEmpById(@PathVariable int id){
        empService.deleteEmpById(id);
            return Result.success();
    }

    /**
     * 员工根据id查询信息
     */
    @GetMapping("/findEmpById")
    @ApiOperation("根据员工id查询员工信息")
    public Result<Emp> findEmpById(int id){
        Emp emp = empService.findEmpById(id);
        return Result.success(emp);
    }

    /**
     * 员工修改信息
     */
    @PutMapping("/updateEmpById")
    @ApiOperation("修改员工信息")
    public Result updateEmpById(@RequestBody EmpDto empDto){
        empService.updateEmpById(empDto);
        return Result.success();
    }






}























